package com.example.config;

import com.example.converter.JsonMessageConverter;
import com.example.converter.StringMessageConverter;
import com.example.entity.CarEntity;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.SearchStrategy;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMessage;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;

import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.List;

@Configuration
public class CommonConfig {
    @Bean
    //@ConditionalOnMissingBean(value = MappingJackson2HttpMessageConverter.class, search = SearchStrategy.CURRENT)
    public MappingJackson2HttpMessageConverter getJsonMessageConvent(@Value("${spring.http.encoding.charset:utf-8}") String encoding) {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
        JsonMessageConverter mappingJackson2HttpMessageConverter = new JsonMessageConverter();
        mappingJackson2HttpMessageConverter.setObjectMapper(objectMapper);

        return mappingJackson2HttpMessageConverter;
    }

//    @Bean
//    //@ConditionalOnMissingBean(value = StringHttpMessageConverter.class, search = SearchStrategy.CURRENT)
//    public StringHttpMessageConverter getStringMessageConvent(@Value("${spring.http.encoding.charset:utf-8}") String encoding) {
//        StringMessageConverter stringMessageConverter = new StringMessageConverter();
//        stringMessageConverter.setDefaultCharset(Charset.forName(encoding));
//        return stringMessageConverter;
//    }
}
